<?php


namespace app\admin\controller;


use app\common\controller\AdminBase;
use app\common\model\Score;
use app\common\model\Task;

class Jdz extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        $status = [
            1 => '已提交',
            2 => '已采纳',
            3 => '未采纳',
            4 => '待完成',
            5 => '已申请复活',
            6 => '未采纳',
            7 => '再次申请复活',
            8 => '总经理未采纳',
            9 => '新补充（待采纳）',
            10 => '新补充（未采纳）',
            11 => '新补充（已采纳）',
            12 => '新补充（以评估）',
            13 => '已完成',
            14 => '已验收(已关闭)',
            15 => '发布成功',
            16 => '未采纳',
            17 => '已申请复活',
            18 => '已采纳',
            19 => '任务完成',
            20 => '提议被撤回(已关闭)'
        ];
        $complete_type = [
            1 => '我提你改',
            2 => '自提自改',
            3 => '寻求合作'
        ];
        $this->assign('status', $status);
        $this->assign('complete_type', $complete_type);
        return $this->fetch();
    }

    public function getLists()
    {
        if ($this->request->isAjax()) {
            $m = new \app\admin\model\Jdz();
            return $m->getLists();
        }
    }

    public function detail()
    {
        $id = input('id');
        $rows = model('jdz')->find($id)->toArray();
        $this->assign('rows', $rows);
        return $this->fetch();
    }

    public function reward()
    {
        $reward = [
            1 => '未发放',
            2 => '已发放'
        ];

        $complete_type = [
            1 => '我提你改',
            2 => '自提自改',
            3 => '寻求合作'
        ];
        $this->assign('complete_type', $complete_type);
        $this->assign('reward', $reward);
        return $this->fetch();
    }


    public function reward_to()
    {
       $complete_type = [
            1 => '我提你改',
            2 => '自提自改',
            3 => '寻求合作'
        ];
        $this->assign('complete_type', $complete_type);
        $this->assign('reward', $reward);
        return $this->fetch();
    }




    public function score()
    {
        $id = input('id');
        $score = new Score();
        $score_list = $score->where(['jdz_id' => $id])->select()->toArray();
        $count1 = 0;
        $sum1 = 0;
        $array1 = [];
        foreach ($score_list as $k => $v) {
            $user[$k] = model('user')->find($v['user_id'])->toArray();
                $sum1 = $sum1 + $v['score'];
                $array1[$k] = $v;
                $array1[$k]['user_name'] = $user[$k]['user_name'];
                $count1+=1;
        }
        $final_score = ($sum1 / $count1)  ;
        $this->assign('final_score', $final_score);
        $this->assign('array1', $array1);
        return $this->fetch();
    }

    public function reward_touser()
    {
        $id = input('id');
        $task = new Task();
        $score = new Score();
        $score_list1 = $score->where(['jdz_id' => $id, ])->column('avg(score) as avg');
        $final_score = floor($score_list1[0]);
        $task_list = $task->alias('t')->where(['jdz_id' => $id])->join('hs_user u', 't.user_id=u.user_id')->select()->toArray();
        $rows = model('jdz')->find($id);
        //提议人是否参与完成提议
        foreach ($task_list as $k => $v) {
            if ($rows['user_id'] == $v['user_id']) {
                $is_flag = 1;
                $task_list[$k]['is_publish'] = 1;
            }
        }
        if ($is_flag == 1) {
            $user_count = count($task_list) + 1;
        } else {
            $user_count = count($task_list);
        }
        switch ($final_score) {
            case $final_score <= 5:
                $amount = 100;
                break;
            case $final_score > 5 && $final_score <= 7:
                $amount = 200;
                break;
            case 8:
                $amount = 300;
                break;
            case 9:
                $amount = 400;
                break;
            case 10:
                $amount = 500;
                break;
            default:
                $amount = 0;
        }
        $amount_user = average_money($amount, $user_count);
        //每个人获取的金额
        foreach ($task_list as $key => $value) {
            $task_list[$key]['amount'] = $amount_user[$key];
            if ($value['is_publish']) {
                $task_list[$key]['amount'] = $task_list[$key]['amount'] + $amount_user[count($amount_user) - 1];
            }
        }
        $this->assign('amount', $amount);
        $this->assign('task_list', $task_list);
        return $this->fetch();
    }

    public function put_reward()
    {
        $amount = input('amount/a');
        $userid = input('userid/a');
        $username = input('username/a');
        $relation_id = input('relation_id');
        $reward = input('reward');
        $rows = model('jdz')->find($relation_id);
        if ($rows['reward'] > 0) {
            return ajax_return(0, '奖金已经发放');
        }
        foreach ($userid as $k => $v) {
            $data[$k]['user_id'] = $v;
            $data[$k]['user_name'] = $username[$k];
            $data[$k]['amount'] = $amount[$k];
            $data[$k]['add_time'] = time();
            $data[$k]['admin_user'] = session('admin_name');
            $data[$k]['relation_id'] = $relation_id;
        }
        if (model('useramount')->saveAll($data) !== false) {
            foreach ($data as $value) {
                model('user')->where(['user_id' => $value['user_id']])->setInc('amount', $value['amount']);
            }
            model('jdz')->where(['id' => $relation_id])->setInc('reward', $reward);
            return ajax_return(1, '发放成功');
        }
        return ajax_return(0, '发放失败');
    }

    public function log()
    {
        $id = input('id');
        $list = model('useramount')->where(['relation_id' => $id])->select();
        $this->assign('list', $list);
        return $this->fetch();
    }


    public function undo(){
        $id = input('id');
        if(model('jdz')->isUpdate(true)->save(['status'=>20],['id'=>$id])!==false){
            return ajax_return(1,'撤回成功');
        }
        return ajax_return(0,'撤回失败');
    }

    public function output(){
        $m = new \app\admin\model\Jdz();
        return $m->output();
    }

    public function output_o(){
        $m = new \app\admin\model\Jdz();
        return $m->output_o();
    }
    public function changes_status(){
        $change_status = [
            15=>'提议部门领导审核',
            18=>'提议部门分管领导审核',
            1=>'问题部门领导审核',
            2=>'问题部门分管领导审核',
            7=>'总经理审核',
            20=>'结束',
            13=>'完成'
        ];
        $this->assign('change_status',$change_status);
        if($this->request->isAjax()){
            $data['status']=input('status');
            $id = input('id');
            if(input('clear_task')==1){
                model('task')->where('jdz_id','=',$id)->delete();
            }
            if(model('jdz')->isUpdate(true)->save($data,['id'=>$id])!==false){
                return ajax_return(1,'提交成功');
            }
            return ajax_return(0,'提交失败');
        }
        return $this->fetch();
    }
}